package gameDisplayer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.List;
import javax.swing.JComponent;
import gameManager.GameManager;
import gameManager.Interactable;

/**
 * This class manages the full Visualization window, mainly the GUI. All
 * the relevant functions are located here.
 * @author Kannan and Xi Du
 *
 */
public class GameDisplayer {

    private Game myGame;
    private JFrame myFrame;
    private GameManager myGameManager;
    private String myLevel;
    private List<Interactable> myInteractableCharacters;
    private Canvas myCanvas;
    private JComboBox c = new JComboBox();

    private static final Dimension SIZE = new Dimension();
    /**
     * The constructor for this class, initializing the JFrame, the gameManager,
     * the interactable characters, and the game itself. Is passed a GameManager.
     * @param m The model side is passed to the view side - the GameDisplayer.
     */
    public GameDisplayer(GameManager m) {
        myGameManager = m;
        myFrame = new JFrame();
        myInteractableCharacters = m.getInteractableObjects();
        myGame = new Game(myInteractableCharacters);
    }

    /**
     * Paints the Game Objects - this function is responsible for updating
     * the illustrations of all the objects as they move.
     */
    public void paintGameObjects(){
        
        //myGame.paintComponent((Graphics2D)pen, SIZE);
        updateDisplay();
    }
    /**
     * Updates and repaints the display.
     */
    public void updateDisplay () {
        JComponent canvas = getGame(SIZE);
        myFrame.getContentPane().removeAll();
        myFrame.getContentPane().add(canvas);
         myFrame.repaint();
        myFrame.setTitle("Game one");
        myFrame.setVisible(true);
    }

    /**
     * Initializes the Jframe
     */
    public void initializeFrame () {
         myFrame.setSize(SIZE);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setMinimumSize(SIZE);
        myFrame.setLocationRelativeTo(null);
        myFrame.setVisible(true);
        myFrame.setFocusable(true);
        myFrame.addKeyListener(new AL());
    }
    
    
    
    public JComponent getGame (Dimension dim) {
        Canvas canvas = new Canvas(dim, myGame);
        return canvas;
    }

    /**
     * Creates and Displays a pop-up menu (a dropdown)
     * which allows the user to choose which level he or
     * she would liek to play.
     */
    public void displayStartupMenu() {
        List<String> levelList = myGameManager.getLevels();
        for (int i = 0; i < levelList.size(); i++)
            c.addItem(levelList.get(i));
          
          c.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                myLevel="" + ((JComboBox) e.getSource()).getSelectedItem();
              System.out.println("" + ((JComboBox) e.getSource()).getSelectedItem());
              myGameManager.setLevel(myLevel);
              initializeFrame();
              myGame.setmyBackGroundImage(myGameManager.getBackgroundImage());
              updateDisplay();
              //drawBackground(myGameManager.getBackgroundImage());
              myGameManager.initializePlayerCharacter();
            }
          });
          JFrame myFrame=new JFrame();
          myFrame.add(c);
          myFrame.setVisible(true);
          myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    /**
     * Draws the Background Image, uploaded by the user.
     * @param pen The graphics/drawing tool to be used in drawing a
     * visualization
     * @param myImage The image to serve as the background
     */
    public void drawBackground(Graphics2D pen,Image myImage){
    myGame.drawBackground(pen, myImage);
    }
    /**
     * The Action Listener Class to manage the user input.
     * @author Xi Du
     *
     */
    private class AL extends KeyAdapter{
    /**
     * Manages the release of keys.
     * @param e The Keyevent to be registered.
     */
    public void keyReleased(KeyEvent e){
        myGameManager.keyReleased(e);
    }
    /**
     * Manages the pressing of keys.
     * @param e The keyevent to be registered
     */
    public void keyPressed(KeyEvent e){
        myGameManager.keyPressed(e);
    }
}    
    
}

